//
//  HHTrameAnimationTabBarController.m
//  Flower
//
//  Created by 花花 on 2017/1/21.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "HHTrameAnimationTabBarController.h"

@interface HHTrameAnimationTabBarController ()

@end

@implementation HHTrameAnimationTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createChildControlViews];
}
-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self addTabBarSelectedImageAnimation];
}

- (void)addTabBarSelectedImageAnimation{
    
    for (UIControl *ctrl in self.tabBar.subviews) {
        if ([ctrl isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [ctrl addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            
        }
    }
}
-(void)tabBarButtonClick:(UIControl *)ctrl{
    UIView *imageView,*label;
    [self addView:imageView ctrl:ctrl classString:@"UITabBarSwappableImageView"];
    [self addView:label ctrl:ctrl classString:@"UITabBarButtonLabel"];
    
}

-(void)addView:(UIView *)view ctrl:(UIControl *)ctrl classString:(NSString *)classString{
    
    for (UIView *view in ctrl.subviews) {
        if ([view isKindOfClass:NSClassFromString(classString)]) {
            
            CAKeyframeAnimation *animation =[CAKeyframeAnimation animation];
            animation.keyPath =@"transform.scale";
            animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
            animation.duration = 1;
            animation.calculationMode = kCAAnimationCubic;
            [view.layer addAnimation:animation forKey:nil];
        }
    }
    
}
-(void)createChildControlViews{
    
    //控制器名字
    NSArray *controllers = @[@"HHBaseViewController",@"HHBaseViewController",@"HHBaseViewController",
                             @"HHBaseViewController"];
    //TabBar图片
    NSArray *normalImgNames = @[@"normal-1",@"normal-2",@"normal-3",@"normal-4"];
    
    //TabBar选中图片
    NSArray *selectedImgNames = @[@"selected-1",@"selected-2",@"selected-3",@"selected-4"];
    
    //TabBarItem标题
    NSArray *titles = @[@"首页",@"消息",@"订单",@"我"];
    
    
    self.tabBar.tintColor  = [UIColor colorWithHexString:@"fa2850"];
    for (int i = 0 ; i<controllers.count; i++) {
        
        //1.创建对象
        UIViewController *Vc  = [[NSClassFromString(controllers[i]) alloc]init];
        
        Vc.tabBarItem.title = titles[i];
        Vc.tabBarItem.image = [[UIImage imageNamed:normalImgNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        Vc.tabBarItem.selectedImage =  [[UIImage imageNamed:selectedImgNames[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        [self addChildViewController:Vc];
    }
}


@end
